﻿Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports IDServer.IDServer
Imports Microsoft.Office


Public Class Frm_SYSOtherDefine
    Implements IDServer.IDServer.IDServer


    Private ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private mDataset As DataSet
    Private mDataset1 As DataSet
    Private SelectNode As TreeNode
    Public bFind As Boolean
    Private Img As ImageList
    Public WithEvents MainFrom As Frm_MainForm
    Private Captionstr As String = "項目編號^ListCode&項目1^ListA&項目2^ListB&項目3^ListC&項目4^ListD"

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainFrom
        End Get
        Set(ByVal value As Frm_MainForm)
            MainFrom = value
        End Set
    End Property

    Private isLoad As Boolean = False


#Region "Interface Define"


    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        If AddEnable = False Then
            MessageBox.Show("對不起，您沒有新增權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

        ActionHelp = ActionBtn.ActionAdd
        Me.txtItemCode.Text = ""
        Me.txtItemName.Text = ""
        SetEnable(True)
        Me.SetButton(ButtonType.Add, False)
        mDataset1.Tables(0).Rows.Clear()
        Me.DataGridView1.DataSource = mDataset1.Tables(0)
        Me.txtItemCode.Focus()

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            ActionHelp = ActionBtn.ActionHelp
            Me.txtItemCode.Text = ""
            Me.txtItemName.Text = ""
            SetEnable(False)
            If Me.TreeView1.SelectedNode.Tag.ToString = "All" Then
                bFind = False
            Else
                FillText(SelectNode.Tag.ToString.Split("^")(1))
                bFind = True
            End If
            'MainFrom.BtnEnable(bFind)
            'GetToolEnable()
            Dim isFind As Boolean = False
            If Not Me.TreeView1.SelectedNode Is Nothing AndAlso Me.TreeView1.SelectedNode.Level > 0 Then
                isFind = True
            End If
            Me.SetButton(ButtonType.Browse, isFind)
        Catch ex As Exception
            MsgBox("ActionCancel：" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        If DeleteEnable = False Then
            MessageBox.Show("對不起，您沒有刪除權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try
            If EditEnable = False Then
                MessageBox.Show("對不起，您沒有修改權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            ActionHelp = ActionBtn.ActionEdit
            SetEnable(True)
            Me.SetButton(ButtonType.Edit, False)
            Me.txtItemCode.Enabled = False
            Me.txtItemName.Enabled = False
        Catch ex As Exception
            MsgBox("ActionEdit：" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        If BtnActionSave() = True Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function


#End Region


#Region "Control Events"

    Private Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Try
            MainFrom.ToolStripCancel_Click(Nothing, Nothing)
        Catch ex As Exception
            MsgBox("BtnCancel_Click" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub BtnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnOk.Click
        Try
            MainFrom.ToolStripSave_Click(Nothing, Nothing)
        Catch ex As Exception
            MsgBox("BtnOk_Click" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Try
            'If e Is Nothing Then
            '    Exit Sub
            'End If
            SelectNode = e.Node
            If e.Action = TreeViewAction.ByKeyboard Or e.Node.Tag.ToString = "All" Then
            Else
                bFind = True
                FillText(Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1))
                Dim Dr() As DataRow = mDataset.Tables(0).Select("ID=" & Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1))
                Select Case e.Node.Tag.ToString.Split("^")(0).ToLower
                    Case "MemberDefine".ToLower
                        Me.Panel3.Visible = False
                        Me.Panel4.Visible = True
                        Me.Panel5.Visible = False
                        Me.Panel6.Visible = False
                        Me.Panel7.Visible = False
                        Me.Panel8.Visible = False
                        Dim Rows() As DataRow = mDataset1.Tables(0).Select("ItemNo=" & Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1))
                        If Rows.Length > 0 Then
                            For Each mRow As DataRow In Rows
                                If mRow("ListCode") = "0" Then
                                    Me.RDBMember1.Checked = True
                                    Me.txtConsume.Text = mRow("ListA") & ""
                                    Me.txtMark.Text = mRow("ListB") & ""
                                ElseIf mRow("ListCode") = "1" Then
                                    Me.RDBMember2.Checked = True
                                    Me.txtConsume1.Text = mRow("ListA") & ""
                                    Me.txtMark1.Text = mRow("ListB") & ""
                                End If
                            Next
                        End If
                    Case "POSSet".ToLower
                        Me.Panel3.Visible = False
                        Me.Panel4.Visible = False
                        Me.Panel5.Visible = False
                        Me.Panel6.Visible = False
                        Me.Panel7.Visible = False
                        Me.Panel8.Visible = True
                        Dim Rows() As DataRow = mDataset1.Tables(0).Select("ItemNo=" & Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1))
                        If Rows.Length > 0 Then
                            For Each mRow As DataRow In Rows
                                If mRow("ListCode") = "0" Then
                                    Me.RDBPosNo.Checked = True

                                ElseIf mRow("ListCode") = "1" Then
                                    Me.RDBPosYes.Checked = True

                                End If
                            Next
                        End If
                    Case "InStoreNum".ToLower
                        Me.Panel3.Visible = False
                        Me.Panel4.Visible = False
                        Me.Panel6.Visible = False
                        Me.Panel5.Visible = True
                        Me.Panel7.Visible = False
                        Me.Panel8.Visible = False
                    Case "OutStoreNum".ToLower
                        Me.Panel3.Visible = False
                        Me.Panel4.Visible = False
                        Me.Panel5.Visible = False
                        Me.Panel7.Visible = False
                        Me.Panel6.Visible = True
                        Me.Panel8.Visible = False
                    Case "EarnestSet".ToLower
                        Dim Rows() As DataRow = mDataset1.Tables(0).Select("ItemNo=" & Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1))
                        If Rows.Length > 0 Then
                            Me.NumericUpDown1.Value = Rows(0).Item("ListCode") & ""
                        End If
                        Me.Panel3.Visible = False
                        Me.Panel4.Visible = False
                        Me.Panel5.Visible = False
                        Me.Panel7.Visible = True
                        Me.Panel6.Visible = False
                        Me.Panel8.Visible = False
                    Case Else
                        Me.Panel4.Visible = False
                        Me.Panel5.Visible = False
                        Me.Panel6.Visible = False
                        Me.Panel7.Visible = False
                        Me.Panel8.Visible = False
                        Me.Panel3.Visible = True
                        Me.DataGridView1.DataSource = mDataset1.Tables(0)
                End Select
                If Dr.Length > 0 Then
                    For Each mRow As DataRow In Dr
                        Me.txtItemCode.Text = mRow("ItemCode") & ""
                        Me.txtItemName.Text = mRow("ItemCName") & ""
                    Next
                End If
                'MainFrom.BtnEnable(bFind)
                'MainFrom.ToolStripDelete.Enabled = False
                'MainFrom.DelToolStripMenuItem.Enabled = False
                'GetToolEnable()
            End If
            If Me.isLoad = True Then
                Me.SetButton(ButtonType.Browse, e.Node.Level > 0)
            End If

        Catch ex As Exception

            MsgBox("TreeView1_AfterSelect" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)

        End Try
    End Sub


    Private Sub Frm_SYSOtherDefine_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            DataBindGrid()
            ''LoadMultiLanguage()
            BindGridCaption(Me.DataGridView1, BindHashTable(Captionstr))
            LoadTreeView(Me.TreeView1)
            SetEnable(False)
            Me.Panel7.Visible = False
            Me.Panel8.Visible = False
            isLoad = True
        Catch ex As Exception
            MsgBox("Frm_SYSOtherDefine_Load:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
    Private Function BindData() As DataSet
        Try
            Dim M_Ds As New DataSet
            Dim mTable As New DataTable
            With mTable
                .Columns.Add("ItemNo")
                .Columns.Add("ItemA")
                .Columns.Add("ItemB")
                .Columns.Add("ItemC")
                .Columns.Add("ItemD")
            End With
            M_Ds.Tables.Add(mTable)
            Return M_Ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function

#End Region


#Region "SelfDefine Events"

    Private Sub DataBindGrid()
        Try
            Dim Str() As String = Captionstr.Split("&")
            Dim mTable As New DataTable
            mDataset1 = New DataSet
            With mTable
                For i As Integer = 0 To Str.Length - 1
                    .Columns.Add(Str(i).Split("^")(1))
                Next
            End With
            mDataset1.Tables.Add(mTable)
            Me.DataGridView1.DataSource = mDataset1.Tables(0)
        Catch ex As Exception

        End Try
    End Sub

    Private Function GoNextCell(ByRef Grid As DataGridView, ByVal Col As Integer, ByVal Row As Integer, ByVal ColCount As Integer) As Boolean
        Try

            Col += 1

            If Col >= ColCount Then
                Row += 1
                Col = 0
            End If

            Row = Grid.CurrentRow.Index
            Col = Grid.CurrentCell.ColumnIndex
            Return True
        Catch ex As Exception
            MsgBox("GoNextCell:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Sub LoadTreeView(ByVal Tv As TreeView)
        Try
            Dim mNode As New TreeNode
            Img = New ImageList()
            Dim Company As String = Application.StartupPath & "\pic\35FLOPPY.BMP"
            Dim bumen As String = Application.StartupPath & "\pic\CLSDFOLD.BMP"
            Dim gangwei As String = Application.StartupPath & "\pic\OPENFOLD.BMP"
            Dim Image As Image = Nothing
            Img.Images.Add(Drawing.Image.FromFile(Company))
            Img.Images.Add(Drawing.Image.FromFile(bumen))
            Img.Images.Add(Drawing.Image.FromFile(gangwei))
            Tv.ImageList = Img
            mNode.Nodes.Clear()
            mNode.Text = "全部項目"
            mNode.Tag = "All"
            mNode.ImageIndex = 0
            Tv.Nodes.Add(mNode)
            SelectNode = mNode
            Me.TreeView1.SelectedNode = SelectNode
            LoadTreeNode(mNode)
            mNode.Expand()
        Catch ex As Exception
            MsgBox("LoadTreeView:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub LoadTreeNode(ByVal Node As TreeNode)
        Try
            Dim sql As String = ""
            sql = "select * from BSC_OtherDefineA "
            mDataset = ReturnDataset(sql)
            If mDataset.Tables(0).Rows.Count > 0 Then
                For Each mRow As DataRow In mDataset.Tables(0).Rows
                    Dim mNode As New TreeNode
                    mNode.Text = mRow("ItemCName")
                    mNode.Tag = mRow("ItemCode") & "^" & mRow("ID")
                    mNode.ImageIndex = 1
                    mNode.SelectedImageIndex = 2
                    Node.Nodes.Add(mNode)
                Next
            End If
        Catch ex As Exception
            MsgBox("LoadTreeNode:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Function BtnActionSave() As Boolean
        Dim SqlConn As New SqlClient.SqlConnection(ConnectionString)
        SqlConn.Open()
        Dim Sqlcmd As SqlClient.SqlCommand = SqlConn.CreateCommand
        Dim SqlTran As SqlClient.SqlTransaction = Nothing
        Dim SqlBulk As SqlClient.SqlBulkCopy

        Try
            Dim sql As String = ""
            Dim ID As String = ""
            Dim M_Ds As New DataSet
            Dim NodeTag As Integer
            'If ActionInitialize() = False Then Exit Function
            Dim ItemCode As String = Me.txtItemCode.Text.Replace(",", "").Replace("'", "")
            Dim ItemName As String = Me.txtItemName.Text.Replace(",", "").Replace("'", "")
            Dim RDBMember As String = ""
            Dim RDBPos As String = ""
            Dim Consume As String = ""
            Dim Mark As String = ""

            Me.DataGridView1.EndEdit()

            If TypeOf Me.DataGridView1.DataSource Is DataTable Then
                CType(Me.DataGridView1.DataSource, DataTable).AcceptChanges()
            End If

            Me.DataGridView1.Update()
            'mDataset1.Tables(0) = CType(Me.DataGridView1.DataSource, DataTable)

            If Me.RDBMember1.Checked = True Then
                RDBMember = Me.RDBMember1.Tag
                Consume = Me.txtConsume.Text
                Mark = Me.txtMark.Text
            End If
            If Me.RDBMember2.Checked = True Then
                RDBMember = Me.RDBMember2.Tag
                Consume = Me.txtConsume1.Text
                Mark = Me.txtMark1.Text
            End If
            If Me.RDBPosYes.Checked = True Then
                RDBPos = Me.RDBPosYes.Tag
            End If
            If Me.RDBPosNo.Checked = True Then
                RDBPos = Me.RDBPosNo.Tag

            End If
            SqlTran = SqlConn.BeginTransaction
            Sqlcmd.Connection = SqlConn
            Sqlcmd.Transaction = SqlTran

            If ActionHelp = ActionBtn.ActionAdd Then
                sql = "Insert Into BSC_OtherDefineA(ItemCode,ItemCName) values('" & ItemCode & "','" & ItemName & "') ;"
                sql += " SELECT @@IDENTITY as ID"
                M_Ds = ReturnDataset(sql)
                '' mRow("ItemCode") & "^" & mRow("ID")
                NodeTag = M_Ds.Tables(0).Rows(0).Item(0)
                For i As Integer = 0 To mDataset1.Tables(0).Rows.Count - 1
                    Dim ItemNo As String = NodeTag
                    Dim ListCode As String = mDataset1.Tables(0).Rows(i).Item("Listcode").ToString.Replace("'", "").Replace(",", "") & ""
                    Dim ListA As String = mDataset1.Tables(0).Rows(i).Item("ListA").ToString.Replace("'", "").Replace(",", "") & ""
                    Dim ListB As String = mDataset1.Tables(0).Rows(i).Item("ListB").ToString.Replace("'", "").Replace(",", "") & ""
                    Dim ListC As String = mDataset1.Tables(0).Rows(i).Item("ListC").ToString.Replace("'", "").Replace(",", "") & ""
                    Dim ListD As String = mDataset1.Tables(0).Rows(i).Item("ListD").ToString.Replace("'", "").Replace(",", "") & ""
                    sql = "Insert Into BSC_OtherDefineB(ItemNo,Listcode,ListA,ListB,ListC,ListD) values(" & ItemNo & ",'" & ListCode & "','" & ListA & "','" & ListB & "','" & ListC & "','" & ListD & "')"
                    With Sqlcmd
                        .CommandType = CommandType.Text
                        .CommandText = sql
                        .ExecuteNonQuery()
                    End With
                Next
                AddToDataSet(NodeTag, ItemCode, ItemName)
                AddToTreeView(Me.TreeView1.Nodes(0), ItemName, NodeTag, ItemCode)
            ElseIf ActionHelp = ActionBtn.ActionEdit Then


                If Me.TreeView1.SelectedNode.Text = "會員設置" Then
                    'sql = "Insert Into BSC_OtherDefineB(ItemNo,Listcode,ListA,ListB) values('" & Me.TreeView1.SelectedNode.Tag & "','" & RDBMember & "','" & Consume & "','" & Mark & "')"
                    sql = " Update BSC_OtherDefineB Set ListCode='" & RDBMember & "',ListA='" & Consume & "',ListB='" & Mark & "' where ItemNo='" & Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1) & "'"
                    With Sqlcmd
                        .CommandType = CommandType.Text
                        .CommandText = sql
                        .ExecuteNonQuery()
                    End With

                ElseIf Me.TreeView1.SelectedNode.Text = "POS設置" Then
                    sql = "update BSC_OtherDefineB Set ListCode= '" & RDBPos & "' where ItemNo = '" & Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1) & "'"

                    With Sqlcmd
                        .CommandType = CommandType.Text
                        .CommandText = sql
                        .ExecuteNonQuery()
                    End With
                    ''End If


                ElseIf Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(0).ToLower = "EarnestSet".ToLower Then
                    sql = "Delete BSC_OtherDefineB where ItemNo='" & Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1) & "'"
                    With Sqlcmd
                        .CommandType = CommandType.Text
                        .CommandText = sql
                        .ExecuteNonQuery()
                    End With
                    sql = "Insert Into BSC_OtherDefineB(ItemNo,ListCode) values(" & Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1) & "," & Me.NumericUpDown1.Value & ")"
                    With Sqlcmd
                        .CommandType = CommandType.Text
                        .CommandText = sql
                        .ExecuteNonQuery()
                    End With
                Else
                    sql = "Delete BSC_OtherDefineB where ItemNo='" & Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1) & "'"
                    With Sqlcmd
                        .CommandType = CommandType.Text
                        .CommandText = sql
                        .ExecuteNonQuery()
                    End With

                    For i As Integer = 0 To mDataset1.Tables(0).Rows.Count - 1
                        Dim ItemNo As String = Me.TreeView1.SelectedNode.Tag.ToString.Split("^")(1)
                        Dim ListCode As String = mDataset1.Tables(0).Rows(i).Item("Listcode").ToString.Replace("'", "").Replace(",", "") & ""
                        Dim ListA As String = mDataset1.Tables(0).Rows(i).Item("ListA").ToString.Replace("'", "").Replace(",", "") & ""
                        Dim ListB As String = mDataset1.Tables(0).Rows(i).Item("ListB").ToString.Replace("'", "").Replace(",", "") & ""
                        Dim ListC As String = mDataset1.Tables(0).Rows(i).Item("ListC").ToString.Replace("'", "").Replace(",", "") & ""
                        Dim ListD As String = mDataset1.Tables(0).Rows(i).Item("ListD").ToString.Replace("'", "").Replace(",", "") & ""
                        sql = "Insert Into BSC_OtherDefineB(ItemNo,Listcode,ListA,ListB,ListC,ListD) values(" & ItemNo & ",'" & ListCode & "','" & ListA & "','" & ListB & "','" & ListC & "','" & ListD & "')"
                        With Sqlcmd
                            .CommandType = CommandType.Text
                            .CommandText = sql
                            .ExecuteNonQuery()
                        End With
                    Next
                End If
            End If
            SqlTran.Commit()
            MsgBox(IIf(ActionHelp = ActionBtn.ActionAdd, My.resources.Addsuccess, My.resources.EditSuccess), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            ActionHelp = ActionBtn.ActionHelp
            SetEnable(False)
            bFind = True
            'Me.SetButton(ButtonType.Add, bFind)
            Me.SetButton(ButtonType.Browse, bFind)
            Return True
        Catch ex As Exception
            SqlTran.Rollback()
            MsgBox("BtnActionSave:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Private Sub AddToDataSet(ByVal Id As Integer, ByVal fCode As String, ByVal fName As String)
        Try
            Dim mRow As DataRow = mDataset.Tables(0).NewRow
            mRow("ID") = ID
            mRow("ItemCode") = fCode
            mRow("ItemCName") = fName
            mDataset.Tables(0).Rows.Add(mRow)
            mDataset.AcceptChanges()
        Catch ex As Exception
            MsgBox("AddToDataSet:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub AddToTreeView(ByVal Node As TreeNode, ByVal NodeText As String, ByVal NodeTag As Integer, ByVal NodeCode As String)
        Try
            Dim mNode As New TreeNode
            mNode.Text = NodeText
            mNode.Tag = NodeCode & "^" & NodeTag
            mNode.ImageIndex = 1
            mNode.SelectedImageIndex = 2
            Node.Nodes.Add(mNode)
        Catch ex As Exception
            MsgBox("AddToTreeView:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub SetEnable(ByVal B As Boolean)
        Try
            Me.txtItemCode.Enabled = B
            Me.txtItemName.Enabled = B
            Me.DataGridView1.Enabled = B
            Me.RDBMember1.Enabled = B
            Me.RDBMember2.Enabled = B
            Me.RDBPosYes.Enabled = B
            Me.RDBPosNo.Enabled = B
            Me.txtConsume.Enabled = B
            Me.txtConsume1.Enabled = B
            Me.txtMark.Enabled = B
            Me.txtMark1.Enabled = B
            Me.TreeView1.Enabled = Not B
            Me.BtnOk.Enabled = B
            Me.BtnCancel.Enabled = B
            Me.NumericUpDown1.Enabled = B
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub FillText(ByVal ID As Integer)
        Try
            Dim sql As String = ""
            mDataset1 = New DataSet
            sql = " select * from BSC_OtherDefineB  where ItemNo=" & ID
            mDataset1 = ReturnDataset(sql)
        Catch ex As Exception
            MsgBox("FillText:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type"></param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try
            Select Case type
                Case ButtonType.Add
                    With MainFrom
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True
                        .ToolStripFind.Enabled = False
                    End With

                Case ButtonType.Browse
                    With MainFrom
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True
                        .ToolStripCheck.Enabled = False
                        .ToolStripUnCheck.Enabled = False
                        .ToolStripComplete.Enabled = False
                        .ToolStripFind.Enabled = IsFind
                    End With

                Case ButtonType.Cancel
                    With MainFrom
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True
                        .ToolStripFind.Enabled = IsFind
                    End With

                Case ButtonType.Edit
                    With MainFrom
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True
                        .ToolStripFind.Enabled = False
                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

#End Region


    Private Sub GetToolEnable()
        Try
            With MainFrom
                .ToolStripAdd.Enabled = AddEnable
                .ToolStripEdit.Enabled = EditEnable
                .ToolStripDelete.Enabled = DeleteEnable
                .ToolStripPrint.Enabled = PrintEnable
                .ToolStripImport.Enabled = ImportEnable
                .ToolStripExport.Enabled = ExportEnable
                .ToolStripFind.Enabled = FindEnable
                .ToolStripCheck.Enabled = CheckEnable
                .ToolStripUnCheck.Enabled = UncheckEnable
                .ToolStripComplete.Enabled = CompleteEnable
                .AddToolStripMenuItem.Enabled = .ToolStripAdd.Enabled
                .Edit1ToolStripMenuItem.Enabled = .ToolStripEdit.Enabled
                .DelToolStripMenuItem.Enabled = .ToolStripDelete.Enabled
                .SaveToolStripMenuItem.Enabled = .ToolStripSave.Enabled
                .CancelToolStripMenuItem.Enabled = .ToolStripCancel.Enabled
                .PrintToolStripMenuItem.Enabled = .ToolStripPrint.Enabled
                .ExportToolStripMenuItem.Enabled = .ToolStripImport.Enabled
                .ImportToolStripMenuItem.Enabled = .ToolStripExport.Enabled
                .FilterToolStripMenuItem.Enabled = .ToolStripFilter.Enabled
            End With

        Catch ex As Exception
            MsgBox("TreeView1_AfterSelect:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub txtConsume_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtConsume.KeyPress, txtConsume1.KeyPress, txtMark.KeyPress, txtMark1.KeyPress
        Try
            If Char.IsControl(e.KeyChar) = True Then Exit Sub
            If Not IsNumeric(e.KeyChar) Then
                e.Handled = True
            Else
                e.Handled = False
            End If
        Catch ex As Exception
            MsgBox("txtConsume_KeyPress:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub RDBMember1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RDBMember1.CheckedChanged, RDBMember2.CheckedChanged
        Try
            If ActionHelp = ActionBtn.ActionAdd Or ActionHelp = ActionBtn.ActionEdit Then
                Select Case sender.Name
                    Case "RDBMember1"
                        Me.txtConsume.Enabled = True
                        Me.txtMark.Enabled = True
                        Me.txtConsume1.Text = ""
                        Me.txtMark1.Text = ""
                        Me.txtConsume1.Enabled = False
                        Me.txtMark1.Enabled = False
                    Case "RDBMember2"
                        Me.txtConsume1.Enabled = True
                        Me.txtMark1.Enabled = True
                        Me.txtConsume.Text = ""
                        Me.txtMark.Text = ""
                        Me.txtConsume.Enabled = False
                        Me.txtMark.Enabled = False
                End Select
            End If
        Catch ex As Exception
            MsgBox("RDBMember1_CheckedChanged:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub txtItemCode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtItemCode.KeyDown, txtItemName.KeyDown
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
            SendKeys.Send("{F4}")
        End If
    End Sub

    Private Sub TreeView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.Click
        Try
            'Me.TreeView1_AfterSelect(Nothing, Nothing)
        Catch ex As Exception
            Exit Sub
        End Try


    End Sub

    Private Sub Panel2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel2.Paint

    End Sub
End Class
